using System;
using WindowsAPI.Service;

namespace WindowsAPI.Windows
{
    public class ZWindowsOrder : ITreeItem<Window>
    {
        /* ------------------------------------------------------------------------------------------ */

        public static Window[] TopLevel
        {
            get
            {
                var list = new WindowsList();
                EnumWindowProc WindowsEnumerator = (hWnd, lParam) =>
                    {
                        Window ww = hWnd;
                        if(ww.Visible && !string.IsNullOrEmpty(ww.Text))
                            list.Add(hWnd);
                        return true;
                    };

                User32.EnumDesktopWindows(IntPtr.Zero, WindowsEnumerator, IntPtr.Zero);
                return list.ToArray();
            }
        }

        /* ------------------------------------------------------------------------------------------ */

        private readonly Window f_Window;

        /* ------------------------------------------------------------------------------------------ */

        public Window Current { get { return (Window)f_Window.Clone(); } }

        public Window Parent
        {
            get
            {
                var hWnd = User32.GetParent(f_Window);
                return hWnd == IntPtr.Zero ? null : new Window(hWnd);
            }
            set { throw new NotSupportedException(); }
        }

        public Window MasterParrent
        {
            get
            {
                var hWnd = f_Window.Handle;
                var h0 = IntPtr.Zero;
                do
                {
                    h0 = User32.GetParent(hWnd);
                    if(h0 != IntPtr.Zero) hWnd = h0;
                } while(h0 != IntPtr.Zero);

                return new Window(hWnd);

                //var lv_Window = f_Window;
                //var lv_Parent = lv_Window.Parent;
                //if(lv_Parent != null)
                //    do
                //    {
                //        lv_Window = lv_Parent;
                //        lv_Parent = lv_Window.Parent;
                //    } while(lv_Parent != null);
                //return lv_Window;
            }
        }

        public Window FirstTop
        {
            get { return new Window(User32.GetWindow(MasterParrent.Handle, GW.HWND_FIRST)); }
        }

        public Window First { get { return GetWindow(GW.HWND_FIRST); } }
        public Window Next { get { return GetWindow(GW.HWND_NEXT); } set { throw new NotSupportedException(); } }
        public Window Prev { get { return GetWindow(GW.HWND_PREV); } set { throw new NotSupportedException(); } }
        public Window Last { get { return GetWindow(GW.HWND_LAST); } }

        public Window Child { get { return GetWindow(GW.CHILD); } set { throw new NotSupportedException(); } }

        /* ------------------------------------------------------------------------------------------ */

        internal ZWindowsOrder(Window Window) { f_Window = Window; }

        /* ------------------------------------------------------------------------------------------ */

        public Window GetWindow(GW type)
        {
            var hWnd = User32.GetWindow(f_Window.Handle, type);
            return hWnd == IntPtr.Zero ? null : new Window(hWnd);
        }

        public bool SetWindowPos(IntPtr hPosition,
            SetWindowPosFlags Flags = SetWindowPosFlags.IgnoreResize | SetWindowPosFlags.IgnoreMove)
        {
            var pos = f_Window.Rectangle;
            return User32.SetWindowPos(f_Window, hPosition, pos.Left, pos.Top, pos.Width, pos.Height, Flags);
        }

        public bool SetWindowPos(InsertAfterEnum Position,
            SetWindowPosFlags Flags = SetWindowPosFlags.IgnoreResize | SetWindowPosFlags.IgnoreMove)
        {
            var pos = f_Window.Rectangle;
            return User32.SetWindowPos(f_Window, Position, pos.Left, pos.Top, pos.Width, pos.Height, Flags);
        }

        /* ------------------------------------------------------------------------------------------ */
    }
}